/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mylhyl.zxing.scanner.scan.init;

import com.mylhyl.zxing.scanner.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.agp.utils.LayoutAlignment;
import ohos.media.image.common.ImageFormat;

/**
 * 相机业务层
 */
public class CameraService {

    /**
     * abilitySlice对象
     */
    private static AbilitySlice abilitySlice;

    /**
     * 相机初始化
     *
     * @param abilitySlice abilitySlice对象
     */
    public static synchronized void initCameraService(AbilitySlice abilitySlice) {
        CameraService cameraService = new CameraService();
        CameraService.abilitySlice = abilitySlice;
        // 初始化相机配置
        CameraInfoConfig.initCameraConfig(abilitySlice.getContext());
        // 创建相机
        cameraService.createCamera();
    }

    /**
     * 创建相机
     */
    private void createCamera() {
        // 获取组件对象
//        SurfaceProvider surfaceProvider = (SurfaceProvider) abilitySlice.findComponentById(ResourceTable.Id_surface_provider);
        StackLayout stackLayout = (StackLayout) abilitySlice.findComponentById(ResourceTable.Id_stack_layout);
        SurfaceProvider surfaceProvider = new SurfaceProvider(abilitySlice.getContext());
        // 放在 AGP 容器组件的顶层
        surfaceProvider.pinToZTop(true);
        // 设置样式
        StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig();
        layoutConfig.alignment = LayoutAlignment.CENTER;
        layoutConfig.setMarginBottom(55);
        // 根据比例，设置相机长宽值
        layoutConfig.width = CameraInfoConfig.cameraSize.width;
        layoutConfig.height = CameraInfoConfig.cameraSize.height;
        surfaceProvider.setLayoutConfig(layoutConfig);
        // 将组件添加到容器中
        stackLayout.addComponent(surfaceProvider);
        // 获取SurfaceOps对象
        SurfaceOps surfaceOps = surfaceProvider.getSurfaceOps().get();
        // 设置像素格式
        surfaceOps.setFormat(ImageFormat.JPEG);
        // 设置屏幕一直打开
        surfaceOps.setKeepScreenOn(true);
        // 添加回调
        surfaceOps.addCallback(new SurfaceOpsCallBack());
        // 拍照按钮添加点击事件
        Button pictureButton = (Button) abilitySlice.findComponentById(ResourceTable.Id_picture_button);
        pictureButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                CameraInit.capture();
            }
        });

    }

    /**
     * surfaceOps回调实现类
     */
    class SurfaceOpsCallBack implements SurfaceOps.Callback {

        @Override
        public void surfaceCreated(SurfaceOps surfaceOps) {
            // 获取surface对象
            Surface surface = surfaceOps.getSurface();
            CameraInit cameraInit = new CameraInit(abilitySlice.getContext(), surface, surfaceOps);
            // 创建相机对象
            boolean state = cameraInit.createCamera();
            if (state) {
            } else {
            }
        }

        @Override
        public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceOps surfaceOps) {

        }
    }

}
